package cn.blnp.net.flowable.boot.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <h3>工作流引擎路由助手</h3>
 *
 * @author <a href="mailto:blnp.yibin@qq.com">lyb</a>
 * @version 1.0
 * @since 2025/7/2 16:42
 */
@Slf4j
@Service
public class IdRouterService {

    private static final String PIKA_PREFIX_PROCESS_ID = "BPM#ENGINE#PROCESSID";
    private static final String PIKA_PREFIX_TASK_ID = "BPM#ENGINE#TASKID";

    @Resource(name = "pikaTemplate")
    private RedisTemplate<String, Object> pikaTemplate;
    @Value("${bpm.engine-name:default}")
    private String engineName;

    /**
     * <p><b>用途:流程实例路由注册<b></p>
     * @author <a href="mailto:blnp.yibin@qq.com">lyb</a>
     * @since 16:45 2025/7/2
     * @params [processInstanceId]
     * @param processInstanceId
     * @return void
    **/
    public void addProcessId(String processInstanceId) {
        put(PIKA_PREFIX_PROCESS_ID, processInstanceId, engineName);
    }

    /**
     * <p><b>用途:任务路由注册<b></p>
     * @author <a href="mailto:blnp.yibin@qq.com">lyb</a>
     * @since 16:45 2025/7/2
     * @params [taskId]
     * @param taskId
     * @return void
    **/
    public void addTaskId(String taskId) {
        put(PIKA_PREFIX_TASK_ID, taskId, engineName);
    }

    /**
     * <p><b>用途:添加路由信息<b></p>
     * @author <a href="mailto:blnp.yibin@qq.com">lyb</a>
     * @since 16:45 2025/7/2
     * @params [key, hashKey, value]
     * @param key
     * @param hashKey
     * @param value
     * @return void
     **/
    private void put(String key, String hashKey, String value) {
        log.info("Insert Pika success.key={},hashKey={}", key, hashKey);
        pikaTemplate.opsForHash().put(key, hashKey, value);
    }

    /**
     * <p><b>用途:根据流程实例ID获取路由信息<b></p>
     * @author <a href="mailto:blnp.yibin@qq.com">lyb</a>
     * @since 16:45 2025/7/2
     * @params [processInstanceId]
     * @param processInstanceId
     * @return java.lang.String
     **/
    public String getProcessEngineName(String processInstanceId) {
        Object value = pikaTemplate.opsForHash()
                .get(PIKA_PREFIX_PROCESS_ID, processInstanceId);
        return value == null ? "" : value.toString();
    }

    /**
     * <p><b>用途:根据任务ID获取路由信息<b></p>
     * @author <a href="mailto:blnp.yibin@qq.com">lyb</a>
     * @since 16:45 2025/7/2
     * @params [taskId]
     * @param taskId
     * @return java.lang.String
     **/
    public String getTaskEngineName(String taskId) {
        Object value = pikaTemplate.opsForHash().get(PIKA_PREFIX_TASK_ID, taskId);
        return value == null ? "" : value.toString();
    }

    /**
     * <p><b>用途:根据流程实例ID删除路由信息<b></p>
     * @author <a href="mailto:blnp.yibin@qq.com">lyb</a>
     * @since 16:45 2025/7/2
     * @params [processInstanceId]
     * @param processInstanceId
     * @return
     **/
    public void deleteProcessId(String processInstanceId) {
        pikaTemplate.opsForHash().delete(PIKA_PREFIX_PROCESS_ID, processInstanceId);
        log.info("Delete Pika success.processInstanceId={}", processInstanceId);
    }

    /**
     * <p><b>用途:根据任务ID删除路由信息<b></p>
     * @author <a href="mailto:blnp.yibin@qq.com">lyb</a>
     * @since 16:45 2025/7/2
     * @params [taskId]
     * @param taskId
     * @return
     **/
    public void deleteTaskId(String taskId) {
        pikaTemplate.opsForHash().delete(PIKA_PREFIX_TASK_ID, taskId);
    }
}
